﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CE.AreaCountry;
using CE.Global;
using CE.TravelInsurance;
using CE.Provider;

public partial class Provider_TravelInsurance_ViewProviderTIAreaCountry : System.Web.UI.Page
{
    string PageName = "CXTI";
    string SubPageName = "CXTIARACTY";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();
        CheckSubPageAccess();

        if (!Page.IsPostBack)
        {
            LoadRegion();

            this.CheckAreaCountry(this, null);
            
            ddlPlanName.Items.Insert(0, new ListItem("Select", "-1"));            
        }

    }

    private void LoadRegion()
    {
        DataTable _dtRegion = General.GetComboItem("RegionArea", true);
        ddlAreaRegion.DataSource = _dtRegion;
        ddlAreaRegion.DataTextField = _dtRegion.Columns[0].ColumnName.ToString();
        ddlAreaRegion.DataValueField = _dtRegion.Columns[1].ColumnName.ToString();
        ddlAreaRegion.DataBind();

        ddlAreaRegion.Items.Insert(0, new ListItem("Select", "-1"));
    }

    protected void CheckAreaCountry(object sender, EventArgs e)
    {
        TAQuoteResultDB _tadb = new TAQuoteResultDB();
        TAQuoteResult _tadetails = new TAQuoteResult();

        //_tadetails = _tadb.GetTASelectSingleItemSelected(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()), ddlPolicyType.SelectedValue.ToString(), ddlLevelOfCoverage.SelectedValue.ToString());

        if (ddlPlanName.SelectedIndex > 0)
        {
            _tadetails = _tadb.GetTAQuoteResult(Convert.ToInt64(ddlPlanName.SelectedValue), ddlPolicyType.SelectedValue.ToString());

            ClearFields();

            if (_tadetails != null)
            {
                //lblPlanName.Text = _tadetails.PlanName;
                lblTAId.Text = _tadetails.TAInsId.ToString();
            }

            if (ddlPlanName.SelectedIndex > 0)
                this.LoadCountriesOnSelectedIndexChanged(this, null);
        }
    }

    protected void GetPlans(object sender, EventArgs e)
    {
        DataTable dtPlans = new DataTable();
        TAQuoteResultDB _tadb = new TAQuoteResultDB();

        dtPlans = _tadb.GetTASelectSingleItemSelectedDT(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()), ddlPolicyType.SelectedValue);

        if (dtPlans.Rows.Count > 0)
        {
            ddlPlanName.DataSource = dtPlans;
            ddlPlanName.DataTextField = "PlanName";
            ddlPlanName.DataValueField = "TAInsId";
            ddlPlanName.DataBind();
        }
        else
        {
            ddlPlanName.Items.Clear();
        }

        ddlPlanName.Items.Insert(0, new ListItem("Select", "-1"));
 
    }

    private void SelectCountryListPerArea(Int64 ARId)
    {
        //AreaCountryInfoDB _areacountryinfodb = new AreaCountryInfoDB();
        //DataTable _dtareacountries = _areacountryinfodb.GetAreaCountryInfoList(ARId, Convert.ToInt64(lblTAId.Text), ddlPolicyType.SelectedValue.ToString());

        TAAreaCountryDB _tadb = new TAAreaCountryDB();
        DataTable _dtareacountries = _tadb.GetTAAreaCountryList(ARId, Convert.ToInt64(lblTAId.Text), ddlPolicyType.SelectedValue.ToString());

        //Response.Write("<script>window.alert('test" + _dtareacountries.Rows.Count.ToString() + "')</script>");

        if (_dtareacountries.Rows.Count > 0)
        {
            for (int i = 0; i < cblCountries.Items.Count; i++)
            {
                foreach (DataRow row in _dtareacountries.Rows)
                {
                    if (cblCountries.Items[i].Value == row["CountryId"].ToString())
                        cblCountries.Items[i].Selected = true;
                   
                }
            }
        }
        
    }

    protected void LoadAllCountries()
    {
        CountryInfoDB _countryinfodb = new CountryInfoDB();
        DataTable _dtcountries = _countryinfodb.GetCountryInfoList();

        cblCountries.DataSource = _dtcountries;
        cblCountries.DataTextField = "Description";
        cblCountries.DataValueField = "CountryId";
        cblCountries.DataBind();

        cblCountries.RepeatColumns = 5;
        cblCountries.RepeatDirection = RepeatDirection.Horizontal;
    }

    protected void LoadCountriesOnSelectedIndexChanged(object sender, EventArgs e)
    {
        lnkbtnSelectAll.Text = "select all countries";
        lblMessage.Text = "";

        pnlArea.Visible = true;
        lnkShowCountries.Text = "hide countries";
        if (ddlAreaRegion.SelectedValue != "-1")
        {
            LoadAllCountries();
            SelectCountryListPerArea(Convert.ToInt64(ddlAreaRegion.SelectedValue.ToString()));
        }
        else
        {
            pnlArea.Visible = false;          
        }
    }

    protected void ShowCountries(object sender, EventArgs e)
    {

        lnkbtnSelectAll.Text = "select all countries";

        if (pnlArea.Visible == true)
        {
            pnlArea.Visible = false;
            lnkShowCountries.Text = "show countries";
        }
        else
        {
            lnkShowCountries.Text = "hide countries";
            pnlArea.Visible = true;
            LoadAllCountries();
            SelectCountryListPerArea(Convert.ToInt64(ddlAreaRegion.SelectedValue.ToString()));

        }
    }

    protected void SelectAllCountries(object sender, EventArgs e)
    {
      
        if (lnkbtnSelectAll.Text == "select all countries")
        {
            for (int i = 0; i < cblCountries.Items.Count; i++)
            {
                cblCountries.Items[i].Selected = true;
            }

            lnkbtnSelectAll.Text = "unselect all countries";
        }
        else
        {
            for (int i = 0; i < cblCountries.Items.Count; i++)
            {
                cblCountries.Items[i].Selected = false;
            }
            lnkbtnSelectAll.Text = "select all countries";
        }

    }

    protected void ibtSubmit_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
             string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
             if (CheckAuthCode(_authcode, 600))
             {
                 if (ddlPlanName.SelectedIndex > 0)
                 {
                     //AreaCountryInfo _areacountryinfo = new AreaCountryInfo();
                     //AreaCountryInfoDB _areacountryinfodb = new AreaCountryInfoDB();

                     //Int64 newacid = 0;
                     //_areacountryinfodb.CheckAreaCountryItem(Convert.ToInt64(ddlAreaRegion.SelectedValue.ToString()), Convert.ToInt64(lblTAId.Text), ddlPolicyType.SelectedValue.ToString());

                     TAAreaCountry _taareacountry = new TAAreaCountry();
                     TAAreaCountryDB _taareacountrydb = new TAAreaCountryDB();

                     Int64 newacid = 0;
                     _taareacountrydb.CheckAreaCountryItem(Convert.ToInt64(ddlAreaRegion.SelectedValue.ToString()), Convert.ToInt64(lblTAId.Text), ddlPolicyType.SelectedValue.ToString());
                     for (int i = 0; i < cblCountries.Items.Count; i++)
                     {
                         if (cblCountries.Items[i].Selected == true)
                         {
                             int _trantype = 2;
                             _taareacountry.AreaCountryId = 0;                            
                             _taareacountry.AreaId = Convert.ToInt64(ddlAreaRegion.SelectedValue.ToString());
                             _taareacountry.ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]);
                             _taareacountry.CoverageLevel = "";// ddlLevelOfCoverage.SelectedValue;
                             _taareacountry.CountryId = Convert.ToInt64(cblCountries.Items[i].Value.ToString());
                             _taareacountry.TaInsId = Convert.ToInt64(lblTAId.Text);
                             _taareacountry.PlanType = ddlPolicyType.SelectedValue.ToString();
                             _taareacountry.RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());

                            _taareacountry.AreaCountryId = _taareacountrydb.Insert(_taareacountry);

                            if (_taareacountry.AreaCountryId > 0)
                            {
                                InsertProviderAuthTransaction(_taareacountry.AreaCountryId, "TIAreaCountry", _trantype, 600, _authcode, _taareacountry.ProviderCompanyId, _taareacountry.RefCEUserId);
                            }

                             newacid = _taareacountry.AreaCountryId;
                         }
                     }

                     lnkbtnSelectAll.Text = "select all countries";

                     lblMessage.Text = "Saved Successfully!";
                 }
                 else
                     lblMessage.Text = "Pls. create a plan under this Policy/Coverage level.";

             }
              else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }
    }

    protected void ClearFields()
    {
        //lblPlanName.Text = "";
        lblMessage.Text = "";
    }

    protected void ibtBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("~/Provider/TravelInsurance/TravelInsuranceHome.aspx");
    }

    protected bool CheckAuthCode(string strAuthCode, int intAuthCodeType)
    {
        bool _retValue = false;
        ProviderAuthCode _authcode = new ProviderAuthCode();
        ProviderAuthCodeDB _authcodedb = new ProviderAuthCodeDB();
        _authcode = _authcodedb.GetProviderAuthCode(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), intAuthCodeType, strAuthCode);
        if (_authcode != null)
        {
            _retValue = true;
        }

        return _retValue;
    }

    protected Int64 InsertProviderAuthTransaction(Int64 TransactionId, string TransactionCode, int TransactionType,
        int AuthCodeType, string AuthCode, Int64 ProviderCompanyId, Int64 RefCEUserId)
    {
        ProviderAuthTransaction _authtrans = new ProviderAuthTransaction();
        ProviderAuthTransactionDB _authtransdb = new ProviderAuthTransactionDB();

        _authtrans.TransactionId = TransactionId;
        _authtrans.TransactionCode = TransactionCode;
        _authtrans.TransactionType = TransactionType;
        _authtrans.AuthCode = AuthCode;
        _authtrans.AuthCodeType = AuthCodeType;
        _authtrans.ProviderCompanyId = ProviderCompanyId;
        _authtrans.RefCEUserID = RefCEUserId;

        _authtrans.AuthTransId = _authtransdb.Insert(_authtrans);
        return _authtrans.AuthTransId;
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccess()
    {
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), SubPageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
        else if (PageAccess == 100)
        {
            ibtSubmit.Enabled = false;
        }
    }

}
